package de.uni.bremen.estudent.lbs.models;

import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class LbsOverlayItem extends OverlayItem {


    /***
     * Represents the
     */
    private String category;
    private JSONArray opening_hours;

    private String contactInformation;

    public LbsOverlayItem(GeoPoint point, String title, String description) {
        super(point, title, description);
    }

    public LbsOverlayItem(GeoPoint point, String title, String description,
            String category, String contactInformation, String opening) {
        super(point, title, description);
        this.category = category;
        this.contactInformation = contactInformation;
        if (opening.length() == 0) {
            this.opening_hours = null;
        } else {
            try {
                this.opening_hours = new JSONArray(opening);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                this.opening_hours = null;
            }

        }
    }


    public String getContactInformation() {
        return this.contactInformation;
    }

    public String getCategory() {
        return this.category;
    }

    public String getOpeningHours() {
        return this.opening_hours.toString();
    }

    public boolean isOpen() {
        if (this.opening_hours == null) {
            return true;
        }
        String[] intToStrDate = { "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa" };

        Date currentDate = new Date();
        int dateInt = (currentDate.getDay() + 6) % 7;
        String key = intToStrDate[currentDate.getDay()];
        String val;
        boolean open = false;
        JSONArray timeObjs = null;
        try {
            JSONObject dayObj = this.opening_hours.getJSONObject(dateInt);
            timeObjs = dayObj.getJSONArray(key);
            if (timeObjs.length() > 0) {
                for (int i = 0; i < timeObjs.length(); i++) {
                    JSONObject time = (JSONObject) timeObjs.get(i);
                    JSONArray timeKeys = time.names();
                    String timeKey = (String) timeKeys.get(0);

                    String timeStr = time.optString(timeKey, "");
                    String[] timeArr = timeStr.split("-");

                    String[] begin = timeArr[0].split(":");
                    String[] end = timeArr[1].split(":");

                    Date beginDate = new Date(currentDate.getYear(),
                            currentDate.getMonth(), currentDate.getDate(),
                            Integer.parseInt(begin[0]),
                            Integer.parseInt(begin[1]));
                    Date endDate = new Date(currentDate.getYear(),
                            currentDate.getMonth(), currentDate.getDate(),
                            Integer.parseInt(end[0]), Integer.parseInt(end[1]));
                    if (beginDate.before(currentDate)
                            && endDate.after(currentDate)) {
                        open = true;
                        break;
                    }
                }
            } else {
                open = false;
            }
        } catch (JSONException e) {
            // Fehlerfall -> sicherheitshalber geöffnet
            open = true;
        }

        return open;
    }
}
